package com.dsjy.yf.address.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsjy.yf.address.config.base.BizException;
import com.dsjy.yf.address.config.base.CommonEnum;
import com.dsjy.yf.address.entity.dataobject.ExclusivezoneDO;
import com.dsjy.yf.address.entity.myenum.OperateEnum;
import com.dsjy.yf.address.mapper.ExclusivezoneMapper;
import com.dsjy.yf.address.service.ExclusivezoneService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;

/**
 * @author whj
 * @version 1.0
 * @date 2021-02-05 9:39
 */
@Service
public class ExclusivezoneServiceImpl implements ExclusivezoneService {

    @Resource
    private ExclusivezoneMapper exclusivezoneMapper;

    @Override
    @Cacheable(value = "exclusivezone", key = "'searchExclusivezoneAll'")
    public List<ExclusivezoneDO> searchExclusivezoneAll() {
        return exclusivezoneMapper.searchExclusivezoneAll();
    }

    @Override
    @Cacheable(value = "exclusivezone", key = "'searchExclusivezoneWithParentCode'+#parentCode+#flag")
    public List<ExclusivezoneDO> searchExclusivezoneWithParentCode(Integer parentCode, Boolean flag) {
        return exclusivezoneMapper.searchExclusivezoneWithParentCode(parentCode, flag);
    }

    @Override
    @Caching(evict = {
            @CacheEvict(value = "exclusivezone", condition = "#exclusivezoneDO.parentCode != null",
                    key = "'searchExclusivezoneAll'"),
            @CacheEvict(value = "exclusivezone", condition = "#exclusivezoneDO.parentCode != null",
                    key = "'searchExclusivezoneWithParentCode'+#exclusivezoneDO.parentCode+'true'"),
            @CacheEvict(value = "exclusivezone", condition = "#exclusivezoneDO.parentCode != null",
                    key = "'searchExclusivezoneWithParentCode'+#exclusivezoneDO.parentCode+'false'")})
    @Transactional(rollbackFor = RuntimeException.class)
    public Integer addUpdDel(String operate, ExclusivezoneDO exclusivezoneDO) {
        Integer result = null;
        if (OperateEnum.ADD.getOperate().equals(operate) && exclusivezoneDO.getExclusivezoneName() != null) {
            result = addExclusivezone(exclusivezoneDO);
        } else if (OperateEnum.UPDATE.getOperate().equals(operate) && exclusivezoneDO.getId() != null) {
            result = updExclusivezone(exclusivezoneDO);
        }
        return result;
    }

    @Override
    public Integer addExclusivezone(ExclusivezoneDO exclusivezoneDO) {
        if (exclusivezoneDO.getParentCode() == null || String.valueOf(exclusivezoneDO.getParentCode()).length() != 6) {
            throw new BizException(CommonEnum.PARAM_ERROR);
        }
        exclusivezoneDO.setCreateTime(new Timestamp(System.currentTimeMillis()));
        return exclusivezoneMapper.addExclusivezone(exclusivezoneDO);
    }


    @Override
    public Integer updExclusivezone(ExclusivezoneDO exclusivezoneDO) {
        exclusivezoneDO.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        return exclusivezoneMapper.updExclusivezone(exclusivezoneDO);
    }

    @Override
    public IPage searchWithPage(Integer pageNum, Integer pageSize, QueryWrapper wrapper) {
        wrapper.setEntityClass(ExclusivezoneDO.class);
        Page<ExclusivezoneDO> page = new Page<>(pageNum, pageSize);
        return exclusivezoneMapper.selectPage(page, wrapper);

    }
}
